﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PlanningGIS.Win.Controls
{
    /// <summary>
    /// 功能概述: 分页查询控件。 <br />
    /// 使用方法：
    /// 1.先设置PageSize【每页显示的记录条数】属性；<br />
    /// 2.注册当前页改变事件PageIndexChanged；<br />
    /// 3.在开始查询，如点击查询按钮时先调用Init方法；<br />
    /// 4.初次查询完成后调用SetRecordCount(具体参数参考函数的说明)。<br />
    /// </summary>
    [DefaultEvent("PageIndexChanged")]
    public partial class CtlPaging : UserControl
    {
        /// <summary>
        /// 创建实例
        /// </summary>
        public CtlPaging()
        {
            InitializeComponent();
            SetEnable();
        }

        /// <summary>
        /// 总记录条数
        /// </summary>
        private int _RecordCount = 0;
        /// <summary>
        /// 获取总记录条数，该属性在初次查询到满足条件的记录总数后通过InitPage进行赋值
        /// </summary>
        [DefaultValue(0)]
        public int RecordCount
        {
            get { return _RecordCount; }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        [DefaultValue(0)]
        public long PageCount
        {
            get;
            private set;
        }

        /// <summary>
        /// 当前页--从1开始
        /// </summary>
        [DefaultValue(0)]
        public long PageIndex
        {
            get;
            private set;
        }

        /// <summary>
        /// 每页的记录条数
        /// </summary>
        [DefaultValue(20)]
        public int PageSize { get; set; }

        /// <summary>
        /// 获取当前页开始的序号
        /// </summary>
        public long StartIndex
        {
            get { return (PageIndex - 1) * PageSize + 1; }
        }

        /// <summary>
        /// 获取当前页结束的序号，从1开始
        /// </summary>
        public long EndIndex
        {
            get { return PageIndex * PageSize; }
        }

        /// <summary>
        /// 是否已经初始化
        /// </summary>
        private bool isInit = false;

        /// <summary>
        /// 
        /// </summary>
        private int _allcount = 0;
        
        /// <summary>
        /// 初始化分页控件。在开始查询之前调用。
        /// 例：点击查询按钮
        /// 1.调用page分页控件.Init();
        /// 2.查询实现的方法...
        /// </summary>
        public void Init()
        {
            isInit = false;
            _RecordCount = 0;
            PageCount = 0;
            PageIndex = 1;
            SetEnable();
        }

        /// <summary>
        /// 设置查询记录的总数。
        /// 
        /// </summary>
        /// <param name="count">查询记录的总数</param>
        /// <param name="isOnlyCount">
        /// 第一次查询时，同时返回记录总数和第一页的记录信息：false；   
        /// 第一次查询时，只放回记录总数：true；
        /// </param>
        /// <param name="allcount">扩展的要显示的记录总数，如count为以选择的总数时则allcount为所有记录的总数</param>
        public void SetRecordCount(int count, bool isOnlyCount, int allcount = 0)
        {
            if (isInit) return;
            isInit = true;
            PageIndex = 1;
            _RecordCount = count;
            _allcount = allcount;
            PageCount = (int)Math.Ceiling(_RecordCount * 1.0 / PageSize);
            if (isOnlyCount)
            {
                SetPageChanged();
            }
            else
            {
                SetEnable();
            }
        }
        
        /// <summary>
        /// 页码改变，触发分页事件
        /// </summary>
        private void SetPageChanged()
        {
            SetEnable();
            if (PageIndexChanged != null)
            {
                PageEventArgs args = new PageEventArgs();
                args.PageIndex = PageIndex;
                args.PageSize = PageSize;
                PageIndexChanged(this, args);
            }
        }
        
        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lnkPageFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PageIndex = 1;
            SetPageChanged();
        }
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lnkPagePrevious_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PageIndex = PageIndex - 1;
            SetPageChanged();
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lnkPageNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PageIndex = PageIndex + 1;
            SetPageChanged();
        }
        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lnkPageLast_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PageIndex = PageCount;
            SetPageChanged();
        }

        /// <summary>
        /// 查询按钮是否可用
        /// </summary>
        private void SetEnable()
        {
            lnkPageFirst.Enabled = PageIndex > 1;
            lnkPagePrevious.Enabled = PageIndex > 1;
            lnkPageNext.Enabled = PageCount > PageIndex;
            lnkPageLast.Enabled = PageCount > PageIndex;
            txtPage.ReadOnly = PageCount > 1;
            txtPage.Text = _RecordCount > 0 ? PageIndex.ToString() : "0";
            lblDetial.Text = string.Format("共{0}条，{1}页", RecordCount, PageCount);
            if (_allcount > 0)
            {
                lblDetial.Text = string.Format("共{0}/{1}条，{2}页", RecordCount, _allcount, PageCount);
            }
            else
            {
                lblDetial.Text = string.Format("共{0}条，{1}页", RecordCount, PageCount);
            }
        }

        /// <summary>
        /// 设置转到第几页的值：即将页码中的全角字符转换为半角字符，并验证输入的页是否不大于总页数
        /// </summary>
        /// <param name="value"></param>
        private void SetPageToValue(string value)
        {
            if (value != "")
            {
                int page = int.Parse(txtPage.Text.Trim());
                if (page >= PageCount)
                {
                    PageIndex = PageCount;
                }
                else if (page <= 1)
                {
                    PageIndex = 1;
                }
                else
                {
                    PageIndex = page;
                }
                SetPageChanged();
            }
        }
        /// <summary>
        /// 键盘按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbPageTo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                SetPageToValue(txtPage.Text.Trim());
            }
            else if (Char.IsDigit(e.KeyChar) || e.KeyChar == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        public event DelegatePageIndexChanged PageIndexChanged = null;

    }
    
}
